package com.xingji.caixia.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by zyh on 2017/9/28.
 */

public class BaseActivity extends AppCompatActivity {

    ProgressDialog progressDialog ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在处理...");
    }

    public void post(RequestParams requestParams, final Integer tag){
        progressDialog.show();
        requestParams.addHeader("Content-Type","application/x-www-form-urlencoded");
        x.http().post(requestParams, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject result) {
                postSuccess(result,tag);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                postError(ex,isOnCallback);
            }

            @Override
            public void onCancelled(CancelledException cex) {
                progressDialog.dismiss();
                postCancelled(cex);
            }

            @Override
            public void onFinished() {
                progressDialog.dismiss();
            }
        });
    }

    public void postSuccess(JSONObject jsonObject,Integer tag){

    }

    public void postError(Throwable ex, boolean isOnCallback){
        Toast.makeText(this,"网络错误",Toast.LENGTH_SHORT).show();
    }

    public void postCancelled(Callback.CancelledException cex){

    }

    public void postFinished(){

    }

}
